package org.acghub.mtdb.common.type;

import org.acghub.mtdb.common.Bean;
import org.acghub.mtdb.common.Naming;
import org.acghub.mtdb.common.Type;

import java.util.Set;

/**
 * @author zhouhe
 */
public abstract class TypeBaseMap extends Type {

    private final Type keyType;
    private final Type valueType;

    public TypeBaseMap(Naming parent, Type keyType, Type valueType) {
        super(parent);
        this.keyType = keyType;
        this.valueType = valueType;
    }

    @Override
    public boolean resolve() {
        if (!super.resolve()) {
            return false;
        }
        if (!keyType.isConstType()) {
            throw new RuntimeException(getName() + ".key must be constant type");
        }
        if (!(valueType.isConstType() || valueType instanceof Bean)) {
            throw new RuntimeException(getName() + ".key must be constant type or bean type");
        }
        return true;
    }

    @Override
    public void depends(Set<Type> types) {
        keyType.depends(types);
        valueType.depends(types);
    }

    @Override
    public boolean isConstType() {
        return false;
    }

    public Type getKeyType() {
        return keyType;
    }

    public Type getValueType() {
        return valueType;
    }
}
